var APP = require("../main/app");
var MNActionButton = api.require('MNActionButton');
var weiXin = api.require('wxPlus');
//'我已注册逸洗APP会员，填写我的邀请码' + $("#invate-code").html() + "，可获得红包券"
module.exports = {
	shareTitle : "医疗考试APP",
	shareDesc : "医疗考试APP",
	shareHost:APP.shareHost,//分享地址
	shareThumbUrl : "",
	F_ShareDestination: 2,//分享目的地 1：微信朋友圈；2：微信个人；
	shareFrome : "",//值为my来自我的邀请好友
	shareText:"",//分享的文字
	shareType : "",//分享类型
	studentId : "",//学员id
	videoId : "",//视频id
	schoolId : "",//学校id
	shareContentUrl : "",
	shareImageUrl: "",
	shareVideoTit:"医疗考试APP",
	shareVideoDesc:"医疗考试APP",
	shareVideoThumb:" ",
	shareVideoUrl:" ",
	Error: {
	    "-1": "未知错误",
	    "1": "apiKey非法",
	    "2": "用户取消",
	    "3": "发送失败",
	    "4": "授权拒绝",
	    "5": "微信服务器返回的不支持错误",
	    "7": "注册SDK失败",
	  },

	// 分享图片
	open : function(param) {
		var that = this;
		if(param.shareFrome) {
			that.shareFrome = param.shareFrome;
		}
		if(param.shareType) {
			that.shareType = param.shareType;
		}
		if(param.studentId) {
			that.studentId = param.studentId;
		}
		if(param.schoolId) {
			that.schoolId = param.schoolId;
		}
		if(param.shareTitle) {
			that.shareTitle = param.shareTitle;
		}
		if(param.shareDesc) {
			that.shareDesc = param.shareDesc;
		}
		if(param.shareThumbUrl) {
			that.shareThumbUrl = param.shareThumbUrl;
		}
		if(param.shareContentUrl) {
			that.shareContentUrl = param.shareContentUrl;
		}
		if(param.shareImageUrl) {
			that.shareImageUrl = param.shareImageUrl;
		}
		// 判断设备类型
		var systemType = api.systemType;
		var colSpacing = "";
		if (systemType == "ios") {
			colSpacing = 90;
		} else{
			colSpacing = 60;
		}
		MNActionButton.open({
		    layout: {
		        row: 1,
		        col: 2,
		        rowSpacing: 10,
		        colSpacing: colSpacing,
		        offset: 0
		    },
		    animation: true,
		    autoHide: true,
		    styles: {
		        maskBg: 'rgba(0,0,0,0.2)',
		        bg: '#fff',
		        cancelButton: {
		            size: 0,
		            // size: 40,
		            bg: '#fff',
		            seperatColor:'', 
		            // seperatColor:'#e7e7e7', 
		            icon: 'widget://static/res/ShareBtn/quxiao.png'
		        },
		        item: {
		            titleColor: '#888',
		            titleHighlight: 'dd2727',
		            titleSize: 12
		        },
		        indicator: {
		            color: '#c4c4c4',
		            highlight: '#9e9e9e'
		        }
		    },
		    items: [{
				icon : 'widget://static/res/ShareBtn/iconfont-weixin.png',
				highlight : 'widget://static/ShareBtn/iconfont-weixin.png',
				title : '微信好友'
			}, {
				icon : 'widget://static/res/ShareBtn/iconfont-pengyouquan.png',
				highlight : 'static/widget://ShareBtn/iconfont-pengyouquan.png',
				title : '朋友圈'
			}]
		}, function(ret) {
		    if (ret) {
		        //alert(JSON.stringify(ret));
		        if(ret.eventType == 'click') {
		        	switch (ret.index) {
		        		//发送给微信朋友
		        		case 0:
		        			that.sendToFriend();
		        		break;
		        		//分享到微信朋友圈
		        		case 1:
		        			that.shareToFriend();
		        		break;
		        	}
		        	
		        }
		    }
		});
	},
	
	isInstalled : function(callback) {
		weiXin.isInstalled(function(ret, err) {
			if (ret.installed) {
				$.isFunction(callback) && callback();
			} else {
				if(err.code == -1){
					API.errorToast('请更新微信后重试');
				}else{
					API.errorToast(that.Error[err.code]);
				}
			}
		});
	},
	weixinRequest : function(type, callback) {
		var that = this;
		var scene = 'timeline';
		if(type) {
			scene = type;
		}
		that.isInstalled(
		    function(ret, err) {
		    	/*
		        weiXin.shareWebpage({
				    scene: scene,
				    contentType: 'web_page',
				    title: that.shareDesc,
				    description: that.shareTitle,
				    thumb: that.shareThumbUrl,
				    contentUrl: that.shareContentUrl,
				}, function(ret, err) {
				    if (ret.status) {
				    	$.isFunction(callback) && callback();
				        //api.alert({ title: '发表微信', msg: '发表成功', buttons: ['确定'] });
				    } else {
				        $api.errorToast(that.Error[err.code]);
				    }
				});
				*/
				//分享图片
				if(!that.shareImageUrl){
					API.errorToast('分享失败，请重试...');
				}
				weiXin.shareImage({
				    apiKey: '',
				    scene: scene,
				    //thumb: 'widget://a.jpg',
				    contentUrl: that.shareImageUrl
				}, function(ret, err) {
				    if (ret.status) {
				        // API.errorToast('分享成功');
				    } else {
						if(err.code == -1){
							API.errorToast('请更新微信后重试');
						}else{
							API.errorToast(that.Error[err.code]);
						}
				    }
				});

		    }
		);	        
	},
	sendToFriend : function() {
		var that = this;
		that.weixinRequest('session', function() {
			API.successToast("发送成功");
		});
	},
	shareToFriend : function() {
		var that = this;
		that.weixinRequest('timeline', function() {
			API.successToast("分享朋友圈成功");
		});
	},
	// 分享视频
	  shareVideo : function(param) {
			  var that = this;
			  // 判断设备类型
			  var systemType = api.systemType;
			  var colSpacing = "";
			  if (systemType == "ios") {
			  	colSpacing = 90;
			  } else{
			  	colSpacing = 60;
			  }
			if(param.videoId) {
			  	that.videoId = param.videoId;
			  }
			  if(param.shareFrome) {
			  	that.shareFrome = param.shareFrome;
			  }
			  if(param.shareType) {
			  	that.shareType = param.shareType;
			  }
			  if(param.studentId) {
			  	that.studentId = param.studentId;
			  }
			  if(param.schoolId) {
			  	that.schoolId = param.schoolId;
			  }
			  if(param.shareTitle) {
			  	that.shareTitle = param.shareTitle;
			  }
			  if(param.shareVideoTit) {
			  	that.shareVideoTit = param.shareVideoTit;
			  }
			  if(param.shareVideoDesc) {
			  	that.shareVideoDesc = param.shareVideoDesc;
			  }
			  if(param.shareVideoThumb) {
			  	that.shareVideoThumb = param.shareVideoThumb;
			  }
			  if(param.shareVideoUrl) {
			  	that.shareVideoUrl = param.shareVideoUrl;
			  }
			  MNActionButton.open({
			      layout: {
			      	    row: 1,
			      	    col: 2,
			      	    rowSpacing: 10,
			      	    colSpacing: colSpacing,
			      	    offset: 0
			      	},
			      styles: {
			          maskBg: 'rgba(0,0,0,0.2)',
			          bg: '#fff',
			          cancelButton: {
			              // size: 40,
						  size: 0,
			              bg: '#fff',
			              seperatColor:'', 
			              // seperatColor:'#e7e7e7', 
			              icon: 'widget://static/res/ShareBtn/quxiao.png'
			          },
			          item: {
			              titleColor: '#888',
			              titleHighlight: 'dd2727',
			              titleSize: 12
			          },
			          indicator: {
			              color: '#c4c4c4',
			              highlight: '#9e9e9e'
			          }
			      },
			      items: [{
			  		icon : 'widget://static/res/ShareBtn/iconfont-weixin.png',
			  		highlight : 'widget://static/ShareBtn/iconfont-weixin.png',
			  		title : '微信好友'
			  	}, {
			  		icon : 'widget://static/res/ShareBtn/iconfont-pengyouquan.png',
			  		highlight : 'widget://static/ShareBtn/iconfont-pengyouquan.png',
			  		title : '朋友圈'
			  	}],
				
			  }, function(ret) {
			      if (ret) {
			          //alert(JSON.stringify(ret));
			          if(ret.eventType == 'click') {
			          	switch (ret.index) {
			          		//发送给微信朋友
			          		case 0:
			          			that.sendVideoToFriend();
			          		break;
			          		//分享到微信朋友圈
			          		case 1:
			          			that.shareVideoToFriend();
			          		break;
			          	}
			          	
			          }
			      }
			  }); 
	},
	// 分享文字
	shareText : function(param) {
			  var that = this;
			  // 判断设备类型
			  var systemType = api.systemType;
			  var colSpacing = "";
			  if (systemType == "ios") {
			  	colSpacing = 90;
			  } else{
			  	colSpacing = 60;
			  }
	
			if(param.shareText) {
				that.shareText = param.shareText;
				}
			  if(param.shareFrome) {
			  	that.shareFrome = param.shareFrome;
			  }
			  if(param.shareType) {
			  	that.shareType = param.shareType;
			  }
			  if(param.studentId) {
			  	that.studentId = param.studentId;
			  }
			  if(param.schoolId) {
			  	that.schoolId = param.schoolId;
			  }
			  if(param.shareTitle) {
			  	that.shareTitle = param.shareTitle;
			  }
			  if(param.shareVideoTit) {
			  	that.shareVideoTit = param.shareVideoTit;
			  }
			  if(param.shareVideoDesc) {
			  	that.shareVideoDesc = param.shareVideoDesc;
			  }
			  if(param.shareVideoThumb) {
			  	that.shareVideoThumb = param.shareVideoThumb;
			  }
			  if(param.shareVideoUrl) {
			  	that.shareVideoUrl = param.shareVideoUrl;
			  }
			  MNActionButton.open({
			      layout: {
			      	    row: 1,
			      	    col: 2,
			      	    rowSpacing: 10,
			      	    colSpacing: colSpacing,
			      	    offset: 0
			      	},
			      styles: {
			          maskBg: 'rgba(0,0,0,0.2)',
			          bg: '#fff',
			          cancelButton: {
			              size: 0,
			              // size: 40,
			              bg: '#fff',
			              seperatColor:'', 
			              // seperatColor:'#e7e7e7', 
			              icon: 'widget://static/res/ShareBtn/quxiao.png'
			          },
			          item: {
			              titleColor: '#888',
			              titleHighlight: 'dd2727',
			              titleSize: 12
			          },
			          indicator: {
			              color: '#c4c4c4',
			              highlight: '#9e9e9e'
			          }
			      },
			      items: [{
			  		icon : 'widget://static/res/ShareBtn/iconfont-weixin.png',
			  		highlight : 'widget://static/ShareBtn/iconfont-weixin.png',
			  		title : '微信好友'
			  	}, {
			  		icon : 'widget://static/res/ShareBtn/iconfont-pengyouquan.png',
			  		highlight : 'widget://static/ShareBtn/iconfont-pengyouquan.png',
			  		title : '朋友圈'
			  	}],
				
			  }, function(ret) {
			      if (ret) {
			          //alert(JSON.stringify(ret));
			          if(ret.eventType == 'click') {
			          	switch (ret.index) {
			          		//发送给微信朋友
			          		case 0:
			          			that.sendTxtToFriend();
			          		break;
			          		//分享到微信朋友圈
			          		case 1:
			          			that.shareTxtToFriend();
			          		break;
			          	}
			          	
			          }
			      }
			  }); 
	},
	// 文字
	weixinRequestTxt : function(type, callback) {
		var that = this;
		var scene = 'timeline';
		if(type) {
			scene = type;
		}
		that.isInstalled(
			function(ret, err) {
				//分享文字
				weiXin.shareText({
				    apiKey: '',
				    scene: scene,
				    text: that.shareText
				}, function(ret, err) {
				    if (ret.status) {
				       API.successToast("分享成功");
				    } else {
						API.successToast("分享失败");
				    }
				});
			}
		);
	},
	sendTxtToFriend : function() {
		var that = this;
		that.weixinRequestTxt('session', function() {
			API.successToast("发送成功");
		});
	},
	shareTxtToFriend : function() {
		var that = this;
		that.weixinRequestTxt('timeline', function() {
			API.successToast("分享朋友圈成功");
		});
	},
	// 视频
	weixinRequestVideo : function(type, callback) {
		var that = this;
		var scene = 'timeline';
		if(type) {
			scene = type;
		}
		if(scene == 'timeline') {
			that.F_ShareDestination = 1;
		}else{
			that.F_ShareDestination = 2;
		}
		// 获取分享标题描述
		// 分享编号 S001:个人视频 ；S002：科目二教学视频 ；S003：排行榜；S004：科目二训练分析； S005：邀请朋友学车
		APP.r.get({
			api: 'ShareSetting/GetShareTitleAndDescription',
			params: {F_ShareCode: that.shareType},
			success: function(ret) {
				if (ret.data.list.length>0) {
					that.shareVideoTit = ret.data.list[0].F_ShareTitle ? ret.data.list[0].F_ShareTitle : '医疗考试APP';//标题
					that.shareVideoDesc = ret.data.list[0].F_ShareDescription ? ret.data.list[0].F_ShareDescription : '医疗考试APP';//描述
				}
				InsertShare();
			}
		});

		// 获取分享share_id
		// 分享体 F_StudentId: 学员id,F_ShareCode:1:个人视频 ；2：科目二教学视频 ；3：排行榜；4：科目二训练分析,F_ShareDestination: 分享目的地 1：微信朋友圈；2：微信个人；
		function InsertShare(){
			// 判断来自我的邀请朋友还是其它位置
			if(that.shareFrome == 'my'){
				that.shareVideoUrl = that.shareHost + "home/index/recruit?school="+that.schoolId+'&inviter_id='+that.studentId + '&video_type=&videoId=' + that.videoId + '&inviter_type = 2';
				that.isInstalled(
					function(ret, err) {
						//分享视频
						weiXin.shareVideo({
							apiKey: '',
							scene: scene,
							title: that.shareVideoTit,
							description: that.shareVideoDesc,
							thumb: 'widget://static/icon/circle-logo.png',
							contentUrl: that.shareVideoUrl
						}, function(ret, err) {
							if (ret.status) {
								// alert('分享成功');
							} else {
							   API.errorToast(that.Error[err.code]);
							}
						});
					}
				);
			}
		}
	},
	sendVideoToFriend : function() {
		var that = this;
		that.weixinRequestVideo('session', function() {
			API.successToast("发送成功");
		});
	},
	shareVideoToFriend : function() {
		var that = this;
		that.weixinRequestVideo('timeline', function() {
			API.successToast("分享朋友圈成功");
		});
	}
	
}